package com.demo.mqtt.zigbee.controller;

import com.demo.mqtt.common.client.MqttConnectionUtils;
import com.demo.mqtt.common.utils.HexConvertUtil;
import com.demo.mqtt.picker.entity.PickerInfo;
import io.swagger.annotations.ApiOperation;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: mqtt
 * @description: 消息发布 - 测试用
 * @author: liuYunKai
 * @create: 2021-03-23 14:50
 */
@RestController
@RequestMapping("/mqtt/pub")
public class MqttPubController {

    Logger logger = LoggerFactory.getLogger(MqttPubController.class);

    @Autowired
    private MqttConnectionUtils mqttConnectionUtils;

    @PostMapping("/pub_message")
    public void pubMessage(String topic, int qos, String message) {

        try {
            logger.info("开始发布消息");

            mqttConnectionUtils.pub4Byte(topic,HexConvertUtil.hexStrToByteArrs(message),qos);
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "下发传感器配置")
    @PostMapping("send_sensor_setting")
    public Boolean sendSensorSetting () {
        return mqttConnectionUtils.sendSensorSetting(new PickerInfo());
    }

}
